package short_semester.questionnaire_planet.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import short_semester.questionnaire_planet.MyUtil.PDFUtil;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.service.editService.QuestionService;
import short_semester.questionnaire_planet.response.PDFResponse.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import static short_semester.questionnaire_planet.MyUtil.PDFUtil.*;



@RestController
@RequestMapping("/api")
public class PDFDownloadController {
    @Autowired
    private QuestionnaireDao questionnaireDao;

    @Autowired
    private QuestionSequenceDao questionSequenceDao;
    
    @Autowired
    private QuestionService questionService;

    @Autowired
    PDFUtil pdfUtil;


    @GetMapping("qamanage/downloadPDF")
    public void downloadPDF(
            @RequestParam("questionnaireId") Integer questionnaireId,
            HttpServletResponse response){
        File file = null;
        try{
            //String path = ".\\template3.pdf";

            Questionnaire questionnaire = questionnaireDao.findById(questionnaireId).get();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            // response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(questionnaire.getTitle()+"_问卷"+ UUID.randomUUID()+".pdf","UTF-8"));
            String path = "./"+questionnaire.getTitle()+"_问卷"+ UUID.randomUUID()+".pdf";
            Document doc = pdfUtil.createPdf(path);
            //生成文件
            pdfUtil.createFile(doc,questionnaire);
            doc.close();
            
            
            file = new File(path);
            byte[] buffer = new byte[1024];
            FileInputStream input = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(input);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while(i != -1){
                os.write(buffer);
                i = bis.read(buffer);
            }
            bis.close();
            input.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        deletePDF(file);

    }

    @Async
    public void deletePDF(File file){
        if(file!=null&&file.exists()){
            file.delete();
        }
    }

}

